package com.we.weauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.we.weauth.vo.DocumentsVo;
import org.springframework.stereotype.Service;
import com.we.weauth.mapper.DocumentsMapper;
import com.we.weauth.entity.Documents;
import com.we.weauth.service.DocumentsService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DocumentsServiceImpl extends ServiceImpl<DocumentsMapper, Documents> implements DocumentsService{

    @Resource
    DocumentsMapper documentsMapper;

    @Override
    public List<DocumentsVo> documentsByall() {
        // 查询主目录文档：parentid ：0
        QueryWrapper<Documents> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId",0);
        List<Documents> documents = documentsMapper.selectList(queryWrapper);
        List<DocumentsVo> documentsVo = documentsTree(documents);
        return documentsVo;
    }

    private List<DocumentsVo> documentsTree(List<Documents> documents){
        List<DocumentsVo> documentsVo = new ArrayList<>();
        for (Documents documents1 : documents) {
            if(documents1.getParentId()!=null) {
                DocumentsVo documentsVo1 = new DocumentsVo();
                 // 查询子目录文档
                QueryWrapper<Documents> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parentId",documents1.getId());
                List<Documents> documents2 = documentsMapper.selectList(queryWrapper);
                documentsVo1.setId(documents1.getId());
                documentsVo1.setPath(documents1.getPath());
                documentsVo1.setType(documents1.getType());
                documentsVo1.setCreateTime(documents1.getCreateTime());
                documentsVo1.setUpdateTime(documents1.getUpdateTime());
                documentsVo1.setParentId(documents1.getParentId());;
                documentsVo1.setChildrenDoc(documentsTree(documents2));
                documentsVo.add(documentsVo1);
            } else {
                // 查询到主目录文档
                DocumentsVo documentsVo1 = new DocumentsVo();
                documentsVo1.setId(documents1.getId());
                documentsVo1.setPath(documents1.getPath());
                documentsVo1.setType(documents1.getType());
                documentsVo1.setCreateTime(documents1.getCreateTime());
                documentsVo1.setUpdateTime(documents1.getUpdateTime());
                documentsVo1.setParentId(documents1.getParentId());
                documentsVo.add(documentsVo1);
            }
        }
        return documentsVo;
    }
}
